<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class RandomHttp extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('randomhttp')
            ->setDescription('the randomhttp command');
    }

    protected function execute(Input $input, Output $output)
    {
        $curlInfo = <<<EOF
        curl 'https://comment.api.163.com/api/v1/products/a2869674571f77b5a0867c3d71db5856/threads/IMJDAT3G0001899O/comments/newList?ibc=newspc&limit=30&showLevelThreshold=200&headLimit=1&tailLimit=2&offset=30&callback=jsonp_1703251711422&_=1703251711423' \
        -H 'Accept: */*' \
        -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6' \
        -H 'Connection: keep-alive' \
        -H 'Cookie: _ntes_nuid=4db02d446e6b561523be5e10227842d2; mail_psc_fingerprint=9369c4679624a423ad8cd7dd43786ee3; nts_mail_user=15210521553@163.com:-1:1; __bid_n=187e072c52a18b03694207; FPTOKEN=O6rucPbV+9AkeVH1aGyjZHNP31N04nYr59NGLbFzE08laYJRDHvojMfAK/VI9+g3b97OW12ZNOds6tJCUIHTSCU8pWxa3DYj6Jgc1NZtfbuLbzxHQNfVqpQQGh1rPfcxwDii86H7oyjVQzDwcsuZ0eS6KTN3QlNAc2BYoGA22MDQlXYTrPmIugmW/bR5eRTrgY6INiNANwCshUwfpAb9hQ2EDmDUuYfO5wLxmfb/RN9cMrkuwreFCbF9hl4+BMCPmj/cuWj/1OMiH4OKN4o8SKpnDOiQDTIxVHlr7KwHMUqnD3F1K9hyCqONs1u4wLPd3ipGQEgW5whvL2JV91TkBNB5p5Kj82etOLJ2/G/niegwkHfNB5c9gzA1BpIAt8R8Eu9AzTB4ojMcKxYCs0hO0g==|MWeDMqq3z1tuuPyNg+u2ms1VioUI3c/H6EEKNrHZmgU=|10|407ec3424d2f287eb855d18c4cc570f0; _ntes_nnid=4db02d446e6b561523be5e10227842d2,1686665854357; pver_n_f_l_n3=a; nteslogger_exit_time=1703172855913; _ntes_origin_from=baidu; NTES_PC_IP=%E5%8C%97%E4%BA%AC%7C%E5%8C%97%E4%BA%AC; ne_analysis_trace_id=1703251670235; s_n_f_l_n3=271c908f3db8e5751703251670237; BAIDU_SSP_lcr=https://www.baidu.com/link?url=2DONXjvunkNIMKz4r2GOBUkBM8haubbvsLpiRHPa_z3&wd=&eqid=fccb1c5a000e0cfd0000000365858ed1; UserProvince=%u5317%u4EAC; Hm_lvt_f8682ef0d24236cab0e9148c7b64de8a=1703172811,1703251678; Hm_lpvt_f8682ef0d24236cab0e9148c7b64de8a=1703251707; pgr_n_f_l_n3=271c908f3db8e57517032517064953764; vinfo_n_f_l_n3=271c908f3db8e575.1.1.1703169502605.1703172856954.1703251711201' \
        -H 'Referer: https://comment.tie.163.com/' \
        -H 'Sec-Fetch-Dest: script' \
        -H 'Sec-Fetch-Mode: no-cors' \
        -H 'Sec-Fetch-Site: same-site' \
        -H 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0' \
        -H 'sec-ch-ua: "Not_A Brand";v="8", "Chromium";v="120", "Microsoft Edge";v="120"' \
        -H 'sec-ch-ua-mobile: ?0' \
        -H 'sec-ch-ua-platform: "Windows"' \
        --compressed
EOF;

        $total = 3;
        $func = function(){
            return rand(1388888, 3188888);
        };
        $this->_handle($curlInfo, $total, $func, $output);
    }

    private function _handle(string $curlInfo, int $total, callable $func, Output $output){
        $list = explode("\\",$curlInfo);
        
        $url = '';
        $headers = [];
        $count = count($list);
        foreach($list as $index=>$line){
            $tmp = explode("'", $line);
            if($index == 0){
                $url = $tmp[1];
            } else {
                if(isset($tmp[1])){
                    $headers[] = $tmp[1];
                }
            }
        }

        $client = new \GuzzleHttp\Client();
        $log = \think\facade\Log::channel('script');
        for($i=0;$i<$total;$i++){
            $time = $func();
            // usleep(800*1000);
            usleep($time);
            $response = $client->request('GET', $url, $headers);
            $output->writeln(json_encode([
                'total'=>$total,
                'index'=>$i+1,
                'http_code'=>$response->getStatusCode(),
                'usleep'=>(string)($time/1000000).'s',
            ]));
            $log->info($response->getBody());
        }
    }
}
